package com.m1book.site;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.m1book.domain.InviteService;
import com.m1book.domain.user.Miser;

@RequestMapping("/relation")
@Controller
@SessionAttributes({"currentUser", "sessionId"})
public class QQInviteController {
	private static final Logger logger = LoggerFactory.getLogger(QQInviteController.class);
	
	@Autowired
	private InviteService inviteService;
	
	@RequestMapping(value = "/toinvite", method = RequestMethod.GET, produces = "text/html")
	public String toInvite(@ModelAttribute("currentUser") Miser miser) {
		logger.info("miser " + miser.getId() + " try to invite friend.");
		return "/relation/toinvite";
	}
	
	@RequestMapping(value = "/invite", method = RequestMethod.GET, produces = "text/html")
	public String invite(@RequestParam(required = true, value = "qq") String qq, 
			@RequestParam(required = true, value = "name") String inviteeName, 
			@ModelAttribute("currentUser") Miser miser, Model model) {
		String inviteSN = inviteService.inviteByQQ(miser, qq, inviteeName, "http://localhost:8080/m1book-site/relation/join");
		model.addAttribute("inviteSN", inviteSN);
		return "/relation/inviteok";
	}

}
